<?php

namespace app\common\model;

use app\common\model\card\Dealer;
use think\Model;


class Merchant extends Model
{

    

    

    // 表名
    protected $name = 'merchant';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'openswitch_text',
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }
    public function getOpenswitchList()
    {
        return ['0' => __('Openswitch 0'), '1' => __('Openswitch 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getOpenswitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['openswitch']) ? $data['openswitch'] : '');
        $list = $this->getOpenswitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /*protected static function init()
    {
        $merchant=self::where("FROM_UNIXTIME(access_time,'%Y-%m-%d')!=CURDATE()")->select();//不是今天的点击
        $update=[];
        if(!empty($merchant)){
            foreach ($merchant as $k=>$v){
                $update[$k]=[
                    "id"=>$v["id"],
                    "access_num"=>0,
                ];
            }
        }
        if(!empty($update)){
            $model=new Merchant();
            $model->saveAll($update);
        }
    }*/

    //获取推广二维码
    public function getPromotionCode($id,$type=4){
        //1分销商 2卡经销商 3区域服务商  4商户
        if($type==1){
            $account=Distributor::where(["id"=>$id])->find();
        }
        if($type==2){
            $account=Dealer::where(["id"=>$id])->find();
        }
        if($type==4){
            $account=$this->where(["id"=>$id])->find();
        }
        if($type==3){
            $account=Regionserviceuser::where(["id"=>$id])->find();//推广店铺
        }
        $res=["promotion_code"=>$account["promotion_code"],"promotion_url"=>$account["promotion_url"]];
        if(empty($account["promotion_code"])){
            /*生成二维码 start*/
            $config = get_addon_config('qrcode');
            $cdnurl = \think\Config::get('upload.cdnurl');
            $text=rtrim($cdnurl,'/')."?id=".$id."&type=".$type;
            //$text="https://txh5.westts.cn/share.html?id=".$id."&type=".$type;
            $share="https://txh5.westts.cn/share.html?id=".$id."&type=".$type;
            $qrcodePath = ROOT_PATH . 'public/uploads/promotioncode/';
            $url="/uploads/promotioncode/";
            if($type==3){
                $text="https://txh5o.westts.cn/merchant#/pages/applyFor/applyFor"."?regionserviceuser_id=".$id;//推广店铺
                $qrcodePath.="/regionservice/";//推广店铺
                $url.="regionservice/";
                $share=$text;
            }
            $qrCode = \addons\qrcode\library\Service::qrcode(["text"=>$text]);
            // 写入到文件
            if (!is_dir($qrcodePath)) {
                @mkdir($qrcodePath);
            }
            if (is_really_writable($qrcodePath)) {
                $filePath = $qrcodePath  .md5("id".$id). '.' . $config['format'];
                $qrCode->writeFile($filePath);
            }
            $url.=md5("id".$id). '.' . $config['format'];
            $url=cdnurl($url);
            /*生成二维码 end*/
            $res=["promotion_code"=>$url,"promotion_url"=>$share];
            $account->save($res);
        }
        if(empty($account["promotion_url"]) && $type!=3){
            $share="https://txh5.westts.cn/share.html?id=".$id."&type=".$type;
            $account->save(["promotion_url"=>$share]);
            $res["promotion_code"]=$account["promotion_code"];
        }
        return $res;
    }

}
